package com.xxxx.vao.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.vao.server.mapper.MenuMapper;
import com.xxxx.vao.server.pojo.Admin;
import com.xxxx.vao.server.pojo.Menu;
import com.xxxx.vao.server.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiongl
 * @since 2021-04-20
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {
        Integer id = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        //查询缓存中是否有数据
        List<Menu> menuList = (List<Menu>) valueOperations.get("menu_" + id);
        if (CollectionUtils.isEmpty(menuList)) {
            menuList = baseMapper.getMenusByAdmin(id);
            valueOperations.set("menu_" + id, menuList);
        }
        return menuList;
    }

    @Override
    public List<Menu> getAllMenusWithRole() {
        return baseMapper.getAllMenusWithRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return baseMapper.getAllMenus();
    }
}
